<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 案例1
        // const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
        // for (const num of numbers) {
        //     if (num % 2) {
        //         console.log(num)
        //     }
        // }
        // const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
        // for (const num of numbers) {
        //     if (num % 2 !== 0) {
        //         console.log(num);
        //     }
        // }

        // 案例2
        // const phrases = ['hello world', 'javascript is fun', 'openai gpt', 'coding is great'];
        // for (const phrase of phrases) {
        //     if (phrase.includes('is')) {
        //         console.log(phrase)
        //     }
        // }
        // const phrases = ['hello world', 'javascript is fun', 'openai gtp', 'coding is great'];
        // for (const phrase of phrases) {
        //     if (phrase.includes('is')) {
        //         console.log(phrase);
        //     }
        // }

        // 案例3
        // const mixedNumbers = [-3, 5, -1, 8, -2, 7];
        // let sum = 0;
        // for (const num of mixedNumbers) {
        //     if (num > 0) {
        //         sum += num;
        //     }
        // }
        // console.log(`Total sum of positive numbers:${sum}`);
        // const mixedNumbers = [-3, -5, -1, 8, -2, 7];
        // let sum = 0;
        // for (const num of mixedNumbers) {
        //     if (num > 0) {
        //         sum += num;
        //     }
        // }
        // console.log(`Total sum of positive numbers:${sum}`);

        // 案例1
        // const person = {
        //     name: 'Alice',
        //     age: 25,
        //     city: 'Wonderland',
        //     occupation: 'Engineer'
        // };
        // for (const key in person) {
        //     if (typeof person[key] === 'string') {
        //         console.log(person[key]);
        //     }
        // }
        // const person = {
        //     name: 'Alice',
        //     age: 25,
        //     city: 'Wonderland',
        //     occupation: 'Engineer'
        // };
        // for (const key in person) {
        //     if (typeof person[key] === 'string') {
        //         console.log(person[key]);
        //     }
        // }

        // 案例2
        // const data = {
        //     a: 10,
        //     b: 20,
        //     c: 'thirty',
        //     d: 40,
        //     e: 'fifty'
        // };
        // let sum = 0;
        // for (const key in data) {
        //     if (typeof data[key] === 'number') {
        //         sum += data[key];
        //     }
        // }
        // console.log(`Total sum of numeric values:${sum}`);

        // const data={
        //     a:10,
        //     b:20,
        //     c:'thirty',
        //     d:40,
        //     e:'fifty'
        // };
        // let sum=0;
        // for(const key in data){
        //     if(typeof data [key]==='number'){
        //         sum+=data[key];
        //     }
        // }
        // console.log(`Total sum of numeric values:${sum}`);



        // 案例3
        // const inventory = {
        //     apples: 5,
        //     oranges: 0,
        //     bananas: 8,
        //     grapes: 2,
        //     cherries: 0
        // };
        // for (const key in inventory) {
        //     if (inventory[key] === 0) {
        //         console.log(`${key}:${inventory[key]}`);
        //     }
        // }

        // const inventory = {
        //     apples: 5,
        //     oranges: 0,
        //     bananas: 8,
        //     grapes: 2,
        //     cherries: 0
        // };
        // for (const key in inventory) {
        //     if (inventory[key] === 0) {
        //         console.log(`${key}:${inventory[key]}`);
        //     }
        // }




        // 案例1
        // const numbers = [-3, 5, -1, 8, -2, 7];
        // let sum = 0;
        // let count = 0;
        // numbers.forEach(num => {
        //     if (num > 0) {
        //         sum += num;
        //         count++;
        //     }
        // })
        // const average = sum / count;
        // console.log(`Average of positive numbers:${average}`);

        // const numbers=[-3,5,-1,8,-2,7];
        // let sum=0;
        // let count=0;
        // numbers.forEach(num=>{
        //     if(num>0){
        //         sum+=num;
        //         count++;
        //     }
        // });
        // const average=sum/count;
        // console.log(`Average of positive numbers:${average}`);


        // 案例2
        // const words = ['apple', 'banana', 'cherry', 'date', 'fig', 'grape'];
        // const minLength = 5;
        // words.forEach(word => {
        //     if (word.length > minLength) {
        //         console.log(word);
        //     }
        // });

        // const words = ['apple', 'banana', 'cherry', 'date', 'fig', 'grape'];
        // const minLength = 5;
        // words.forEach(word => {
        //     if (word.length > minLength) {
        //         console.log(word);
        //     }
        // })


        // 案例1
        // const numbers = [-3, 5, -1, 8, -2, 7];
        // const positiveNumbers = numbers.map(num => {
        //     return num < 0 ? -num : num;
        // });
        // console.log(positiveNumbers);

        // const numbers = [-1, 5, -1, 8, -2, 7];
        // const positiveNumbers = numbers.map(num => {
        //     return num < 0 ? -num : num;
        // });
        // console.log(positiveNumbers)

        // 案例2
        // const people = [
        //     { name: 'Alice', age: 25 },
        //     { name: 'Bob', age: 30 },
        //     { name: 'Charlie', age: 35 }
        // ];
        // const uppercasedNames = people.map(person => {
        //     return person.name.length > 3 ? {
        //         ...person, name: person.name.toUpperCase()
        //     } : person;
        // });
        // console.log(uppercasedNames);

        // const people = [
        //     { name: 'Alice', age: 25 },
        //     { name: 'Bob', age: 30 },
        //     { name: 'Charlie', age: 35 }
        // ];
        // const uppercasedNames = people.map(person => {
        //     return person.name.length > 3 ? { ...person, name: person.name.toLocaleUpperCase() } : person;
        // });
        // console.log(uppercasedNames);

        // 案例3
        // const phrases = ['hello world', 'javascript is fun', 'hello openai', 'coding is great'];
        // const updatedPhrases = phrases.map(phrase => {
        //     return phrase.includes('hello') ? phrase.replace('hello', 'hi') : phrase;
        // });
        // console.log(updatedPhrases);

        // const phrases = ['hello world', 'javascript is fun', 'hello openai', 'coding is great'];
        // const updatedPhrases = phrases.map(phrase => {
        //     return phrase.includes('hello') ? phrase.replace('hello', 'hi') : phrase;
        // });
        // console.log(updatedPhrases)


        // 案例1
        // const phrases = ['hello world', 'javascript', 'openai', 'coding is fun'];
        // const totalLength = phrases.filter(phrase => phrase.length > 5).map(phrase => phrase.length).reduce((sum, length) => sum + length, 0);
        // console.log(`Total length of strings longer than 5 characters:${totalLength}`);

        // const phrases = ['hello world', 'javascript', 'openai', 'coding is fun'];
        // const totalLength = phrases.filter(phrase => phrase.length > 5).map(phrase => phrase.length).reduce((sum, length) => sum + length, 0);
        // console.log(`Total length of strings longer than 5 characters:${totalLength}`);

        // 案例2
        // const numbers = [-5, 3, -2, 4, 6, -1];
        // const squareSum = numbers.filter(num => num > 0).map(num => num * num).reduce((sum, squared) => sum + squared, 0);
        // console.log(`Sum of squares of positive numbers:${squareSum}`);

        // const numbers = [-5, 3 - 2, 4, 6, -1];
        // const squareSum = numbers.filter(num => num > 0).map(num => num * num).reduce((sum, squared) => sum + squared, 0);
        // console.log(`Sum of squares of positive numbers:${squareSum}`);

        // 案例3
        // const users = [
        //     { name: 'Alice', age: 22 },
        //     { name: 'Bob', age: 17 },
        //     { name: 'Charlie', age: 30 },
        //     { name: 'David', age: 15 }
        // ];
        // const averageAge = users.filter(user => user.age > 18).map(user => user.age).reduce((sum, age, _, array) => sum + age / array.lenth, 0);
        // console.log(`Average age of users older than 18:${averageAge}`);

        // const users = [
        //     { name: 'Alice', age: 22 },
        //     { name: 'Bob', age: 17 },
        //     { name: 'Charlie', age: 30 },
        //     { name: 'David', age: 15 }
        // ];
        // const averageAge = users.filter(user => user.age > 18).map(user => user.age).reduce((sum, age, _, array) => sum + age / array.length, 0);
        // console.log(`Average age of users older than 18:${averageAge}`);

        // 案例1
        // const cart = [
        //     { id: 1, name: 'Laptop', quantity: 1 },
        //     { id: 2, name: 'Mouse', quantity: 2 },
        //     { id: 3, name: 'Keyboard', quantity: 1 }
        // ];
        // const itemIddToRemove = 2;
        // const indexToRemove = cart.findIndex(item => item.id === itemIddToRemove);
        // if (indexToRemove !== -1) {
        //     cart.splice(indexToRemove, 1);
        // }
        // console.log(cart);

        // const cart = [
        //     { id: 1, name: 'Laptop', quantity: 1 },
        //     { id: 3, name: 'Mouse', quantity: 2 },
        //     { id: 3, name: 'Keyboard', quantity: 1 }
        // ];
        // const itemIddToRemove = 2;
        // const indexToRemove = cart.findIndex(item => item.id === itemIddToRemove);
        // if (indexToRemove !== -1) {
        //     cart.splice(indexToRemove, 1);
        // }
        // console.log(cart);


        // 案例2
        // const employees = [
        //     { id: 1, name: 'John', role: 'Developer' },
        //     { id: 2, name: 'Jane', role: 'Designer' },
        //     { id: 3, name: 'Tom', role: 'Manager' }
        // ];
        // const employeeIdToUpdata = 3;
        // const newRole = 'Senior Manager';
        // const indexToUpdate = employees.findIndex(employee => employee.id === employeeIdToUpdata);
        // if (indexToUpdate !== -1) {
        //     employees[indexToUpdate].role = newRole;
        // }
        // console.log(employees);

        // const employees = [
        //     { id: 1, name: 'John', role: 'Developer' },
        //     { id: 2, name: 'Jane', role: 'Designer' },
        //     { id: 3, name: 'Tom', role: 'Manager' }
        // ];
        // const employeeIdToUpdata = 3;
        // const newRole = 'Senior Manager';
        // const indexToUpdate = employees.findIndex(employee => employee.id === employeeIdToUpdata);
        // if (indexToUpdate !== -1) {
        //     employees[indexToUpdate].role = newRole;
        // }
        // console.log(employees);

        // 案例3
        // const numbers = [4, -1, 6, -2, 7];
        // const indexToReplace = numbers.findIndex(num => num < 0);
        // if (indexToReplace !== -1) {
        //     numbers.splice(indexToReplace, 1, 0);
        // }
        // console.log(numbers);

        // const numbers = [4, -1, 6, -2, 7];
        // const indexToReplace = numbers.findIndex(num => num < 0);
        // if (indexToReplace !== -1) {
        //     numbers.splice(indexToReplace, 1, 0);
        // }
        // console.log(numbers);

        // 扩展
        // const numbers = [1, 2, 3, 4, 5];
        // const sliced = numbers.slice(1, 4);
        // console.log(sliced);

        // const numbers = [1, 2, 3, 4, 5];
        // const sliced = numbers.slice(1, 4);
        // console.log(sliced);


    </script>
</body>

</html>